<?php
	class CompanyModel extends Model{
	
		public function get_company_style($cid){
			$company = D('company_info')->where('cid = '.$cid)->find();
			$diyinfo['style'] = $company['diystyle'];
			$diyinfo['tool'] = explode(',',$company['diytool']);
			$diyinfo['bg'] = get_img_url($company['diybg']);
			$diyinfo['logo'] = get_img_url($company['logo']);
			return $diyinfo;
		}
		 public function get_company_info($cid){
            $info = D('company_info')->where(array('cid'=>$cid,'is_del'=>0,'status'=>1))->find();
            $info['pic'] = get_img_url($info['logo']);
            return $info;
        }
        public function get_company_message($cid){
            $mess = D('message')->where(array('to'=>$cid,'is_del'=>0))->findpage(20);
            foreach($mess['data'] as $k=>&$v){
                $v['reply'] = D('message_reply')->where(array('mid'=>$v['mid'],'is_del'=>0))->select();
                if(eregi("^[0-9]+$",$v['from'])){
					 $v['fr'] = substr_replace($v['from'],'****',3,4);
				}else{
					$v['fr'] = substr_replace($v['from'],"***",6);
				}

             }
            return $mess;
        }
		public function get_hot_hiring_list($cid){
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring.cid = '.$cid.' AND '.C('DB_PREFIX').'hiring.status = 1')
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->select();
			foreach($hirings as $k => &$v){
				$v['sex'] = format_xingbie($v['sex']);
				$v['age'] = explode(',',$v['age']);
				$v['exper'] = format_exper($v['exper']);
				$stores = D('store')->where('sid IN('.$v['workplace'].')')->select();
				foreach($stores as $a => $b){
					$place[] = getareaname($b['pro']).getareaname($b['city']).getareaname($b['area']);
				}
				$v['sname'] = implode('、',array_unique($place));
			}
			return $hirings;
		}
		
		public function get_company_info_bymanage($from){
			$data['wx_id'] = $from;
			$bindinfo = D('company_manage')->where($data)->find();
			$companyinfo = D('company')
							->join(C('DB_PREFIX').'company_info ON '.C('DB_PREFIX').'company.cid = '.C('DB_PREFIX').'company_info.cid')
							->where(C('DB_PREFIX').'company.cid = '.$bindinfo['cid'])
							->find();
			return array('company'=>$companyinfo,'manage'=>$bindinfo);
		}
		
		public function get_company_info_bybid($bid){
			$data['bid'] = $bid;
			$bindinfo = D('company_manage')->where($data)->find();
			$companyinfo = D('company')
							->join(C('DB_PREFIX').'company_info ON '.C('DB_PREFIX').'company.cid = '.C('DB_PREFIX').'company_info.cid')
							->where(C('DB_PREFIX').'company.cid = '.$bindinfo['cid'])
							->find();
			return array('company'=>$companyinfo,'manage'=>$bindinfo);
		}
		
		public function get_company_hiring_list(){
			$cid = $_SESSION['company']['cid'];
			$hiring = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.cid = '.$cid.' AND '.C('DB_PREFIX').'hiring.is_del = 0')
						->order(C('DB_PREFIX').'hiring.updateline DESC')
						->select();
			return $hiring;
		}

		public function count_hiring_check(){
			$cid = $_SESSION['company']['cid'];
			$count = D('hiring')->where('cid ='.$cid)->getField('check',true);
			return $count;
		}
		
		public function get_company_resume_list($send_id,$cid){
			if(!empty($send_id)){
				$dd['is_check'] = 1;
				D('send_resume')->where('id ='.$send_id)->save($dd);
				$resume = D('send_resume')
							->join(C('DB_PREFIX').'resume ON '.C('DB_PREFIX').'resume.resume_id = '.C('DB_PREFIX').'send_resume.resume_id')
							->join(C('DB_PREFIX').'member_info ON '.C('DB_PREFIX').'resume.uid = '.C('DB_PREFIX').'member_info.uid')
							->where(C('DB_PREFIX').'send_resume.id = '.$send_id.' AND '.C('DB_PREFIX').'send_resume.is_del = 0')
							->select();
			}else{
				$hiid = D('hiring')->where(array('cid'=>$cid))->getField('hiring_id',true);
				$hiringid = implode(',', $hiid);
				$job =  D('hiring')->where(array('cid'=>$cid))->getField('hiring_id','title');
				$resume = D('send_resume')
							->join(C('DB_PREFIX').'resume ON '.C('DB_PREFIX').'resume.resume_id = '.C('DB_PREFIX').'send_resume.resume_id')
							->join(C('DB_PREFIX').'member_info ON '.C('DB_PREFIX').'resume.uid = '.C('DB_PREFIX').'member_info.uid')
							->where(C('DB_PREFIX').'send_resume.is_del = 0 AND '.C('DB_PREFIX').'send_resume.hiring_id in('.$hiringid.')' )
							->select();
			}
			foreach($resume as $k => &$v){
				$v = jlformat($v);
				$v['pic'] = get_img_url($v['face']);
				$v['jobname'] = D('hiring')->where(array('hiring_id'=>$v['hiring_id']))->getField('title');
				$v['exper'] = D('exper')->where('uid = '.$v['uid'])->select();
				foreach($v['exper'] as $a => &$b){
					$b['jobbegin'] = strtotime($b['jobbegin']);
					$b['jobend'] = strtotime($b['jobend']);
				}
			}
			//header("Content-type: text/html; charset=utf-8");dump($resume);
			return $resume;
		}
		
		public function order_audition($data){
			$map = array(
						'is_check' => 2,
						'mtime' => $data['mtime'],
						'time' => $data['time'],
						'manage' => $data['manage'],
						'phone' => $data['phone'],
						'address' => $data['address']
					);
			$result = D('send_resume')->where('id = '.$data['send_id'])->save($map);
			if($result){
				$send_info = D('send_resume')->where('id = '.$data['send_id'])->find();
				$hiring_info = D('hiring')
									->join(C('DB_PREFIX').'company ON '.C('DB_PREFIX').'company.cid = '.C('DB_PREFIX').'hiring.cid')
									->join(C('DB_PREFIX').'company_info ON '.C('DB_PREFIX').'company_info.cid = '.C('DB_PREFIX').'hiring.cid')
									->where(C('DB_PREFIX').'hiring.hiring_id = '.$send_info['hiring_id'])
									->find();
				$resume_info = D('resume')
									->join(C('DB_PREFIX').'member_info ON '.C('DB_PREFIX').'resume.uid = '.C('DB_PREFIX').'member_info.uid')
									->join(C('DB_PREFIX').'member ON '.C('DB_PREFIX').'resume.uid = '.C('DB_PREFIX').'member.uid')
									->where(C('DB_PREFIX').'resume.resume_id = '.$send_info['resume_id'])
									->find();
				$data = array(
							'first' => array('value'=>$resume_info['truename']."，你好！\n你收到公司面试通知啦！",'color'=>'#000'),
							'job' => array('value'=>$hiring_info['title'],'color'=>'#000'),
							'company' => array('value'=>$hiring_info['company_name'],'color'=>'#000'),
							'time' => array('value'=>$send_info['mtime'].' '.$send_info['time'],'color'=>'#000'),
							'address' => array('value'=>$send_info['address'],'color'=>'#000'),
							'remark' => array('value'=>'记得准时参加哦~','color'=>'#000')
						);
				$wxbinds = D('hiring_info')->where('hiring_id = '.$send_info['hiring_id'])->find();
				$notices = D('company_manage')->where('bid IN('.$wxbinds['notice'].')')->select();
				$data['contact'] = array('value'=>$send_info['manage'],'color'=>'#000');
				$data['tel'] = array('value'=>$send_info['phone'],'color'=>'#000');
				$url = 'http://www.rencailaile.com'.U('Send_resume/send');
				weixinmessage('mianshi',$data,$resume_info['wx_id'],$url);
				return true;
			}else{
				return false;
			}
		}
		
		public function fly_resume($data){
			$map = array(
						'is_audition' => 0,
					);
			$result = D('send_resume')->where('id = '.$data['send_id'])->save($map);
			if($result){
				return true;
			}else{
				return false;
			}
		}
		
		public function esc_resume($data){
			$map = array(
						'is_del' => 1,
					);
			$result = D('send_resume')->where('id = '.$data['send_id'])->save($map);
			if($result){
				return true;
			}else{
				return false;
			}
		}
		
		public function count_company_resume($hiring_id){
			$count = D('send_resume')->where('hiring_id = '.$hiring_id)->count();
			return $count;
		}
		
		public function count_company_hiring(){
			$cid = $_SESSION['company']['cid'];
			$hiring = D('hiring')->where('cid = '.$cid)->Field('hiring_id')->select();
			foreach($hiring as $k => $v){
				$hiring_ids[] = $v['hiring_id'];
			}
			$count = D('send_resume')->where('hiring_id IN('.implode(',',$hiring_ids).')')->count();
			return $count;
		}
		
		public function list_store(){
			$cid = $_SESSION['company']['cid'];
			$map['cid'] = $cid;
			$map['is_del'] = 0;
			$stores = D('store')->where($map)->findpage(20);
			return $stores;
		}

		public function get_manage_list(){
			$cid = $_SESSION['company']['cid'];
			$manage = D('company_manage')->where('cid = '.$cid.' AND is_del = 0')->select();
			return $manage;
		}
		
		public function get_hiring_template($tid = NULL){
			$cid = $_SESSION['company']['cid'];
			if($tid ==NULL){
				$templates = D('hiring_template')->where('is_del = 0 AND cid = '.$cid)->select();
			}else{
				$templates = D('hiring_template')->where('is_del = 0 AND template_id = '.$tid)->select();
			}
			
			foreach($templates as $k => &$v){
				$age = explode(',',$v['age']);
				$v['age_start'] = $age[0];
				$v['age_end'] = $age[1];
			}
			return $templates;
		}
		
		public function add_hiring_template($data){
			$data['cid'] = $_SESSION['company']['cid'];
			$data['age'] = $data['age_start'].','.$data['age_end'];
			$data['updateline'] = time();
			$template_id = D('hiring_template')->add($data);
			return $template_id;
		}
		
		public function add_hiring($data){
			$data['brand'] = implode(',',$data['brand']);
			$data['workplace'] = implode(',',$data['workplace']);
			$data['hid'] = $data['hangye'];
			$data['pid'] = $data['position'];
			$data['cid'] = $_SESSION['company']['cid'];
			$data['useip'] = '127.0.0.1';
			$data['status'] = 1;
			$data['is_del'] = 0;
			$data['ctime'] = time();
			$data['etime'] = time();
			$data['updateline'] = time();
			$hiring_id = D('hiring')->add($data);
			return $hiring_id;
		}
		
		public function add_hiring_info($data){
			$data['updateline'] = time();
			$data['age'] = $data['age_start'].','.$data['age_end'];
			$result = D('hiring_info')->add($data);
			if($result){
				return true;
			}else{
				return false;
			}
		}
		
		public function get_hiring_info($hid=NULL){
			$cid = $_SESSION['company']['cid'];
			$hirings = D('hiring')
						->join(C('DB_PREFIX').'hiring_info ON '.C('DB_PREFIX').'hiring.hiring_id = '.C('DB_PREFIX').'hiring_info.hiring_id')
						->where(C('DB_PREFIX').'hiring.is_del = 0 AND '.C('DB_PREFIX').'hiring_info.hiring_id = '.$hid)
						->find();
			$age = explode(',',$hirings['age']);
			$hirings['age_start'] = $age[0];
			$hirings['age_end'] = $age[1];
			return $hirings;
		}
		
		public function save_hiring($data,$hiring_id){
			$data['brand'] = implode(',',$data['brand']);
			$data['workplace'] = implode(',',$data['workplace']);
			$data['hid'] = $data['hangye'];
			$data['pid'] = $data['position'];
			$data['useip'] = '127.0.0.1';
			$data['status'] = 0;
			//$data['updateline'] = time();
			$hiring_id = D('hiring')->where('hiring_id = '.$hiring_id)->save($data);
			return $hiring_id;
		}
		
		public function save_hiring_info($data,$hiring_id){
			//$data['updateline'] = time();
			$data['age'] = $data['age_start'].','.$data['age_end'];
			$result = D('hiring_info')->where('hiring_id = '.$hiring_id)->save($data);
			if($result){
				return true;
			}else{
				return false;
			}
		}
	}
?>